package com.gongzuozhan.service.impl;

import com.gongzuozhan.mapper.ChuFangMapper;
import com.gongzuozhan.mapper.GuaHaoJiLuMapper;
import com.gongzuozhan.pojo.BingLi;
import com.gongzuozhan.mapper.BingLiMapper;
import com.gongzuozhan.pojo.ChuFang;
import org.springframework.stereotype.Service;
import com.gongzuozhan.service.BingLiService;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 病例表(BingLi)表服务实现类
 *
 * @author Gold
 * @since 2021-03-23 10:18:26
 * @version 1.0
 */
@Service("bingLiService")
public class BingLiServiceImpl implements BingLiService {
    @Resource
    private BingLiMapper bingLiMapper;

     @Resource
     private ChuFangMapper chuFangMapper;

      @Resource
      private GuaHaoJiLuMapper guaHaoJiLuMapper;
    /**
     * 查询所有数据
     * @return  返回所有数据
     */
	@Override
    public Map<String, Object> chaXunAll(String guaHaoJiLuId) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("list", this.bingLiMapper.chaXunAll(guaHaoJiLuId));
        return map;
    }


    /**
     * 新增数据
     *
     * @param bingLi 实例对象
     * @return 实例对象
     */
	@Override
    public Map<String, Object> xinZeng(BingLi bingLi) {
        Map<String, Object> map = new HashMap<>();
        int jiuZhenJiLuZhuangTai = bingLi.getGuaHaoJiLu().getJiuZhenJiLuZhuangTai();
        String GuaHaoDanHao = bingLi.getGuaHaoJiLu().getGuaHaoDanHao();
        System.out.println(GuaHaoDanHao);
        System.out.println("............................................"+bingLi.getGuaHaoJiLuId());
        String ciSu = this.guaHaoJiLuMapper.chaxun(GuaHaoDanHao);
        Integer zBook = bingLi.getVo().getzBook();
        if (jiuZhenJiLuZhuangTai == 1) {
            if (zBook == 0) {
                this.bingLiMapper.xinZeng(bingLi);
                List<ChuFang> chuFangList = bingLi.getChuFangList();
                for (int i = 0; i < chuFangList.size(); i++) {
                    ChuFang chuFang = chuFangList.get(i);
                    chuFang.setCiSu(Integer.parseInt(ciSu));
                    this.chuFangMapper.xinZeng(chuFang);
                }
                jiuZhenJiLuZhuangTai = 2;
                this.guaHaoJiLuMapper.gengXinById(GuaHaoDanHao, jiuZhenJiLuZhuangTai);

                map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
                map.put("msg", "新增成功");
            }
            if (zBook == 1) {
                this.bingLiMapper.xinZeng(bingLi);
                List<ChuFang> chuFangList = bingLi.getChuFangList();
                for (int i = 0; i < chuFangList.size(); i++) {
                    ChuFang chuFang = chuFangList.get(i);
                    chuFang.setCiSu(Integer.parseInt(ciSu));
                    this.chuFangMapper.xinZeng(chuFang);
                }
                jiuZhenJiLuZhuangTai = 3;
                this.guaHaoJiLuMapper.gengXinById(GuaHaoDanHao, jiuZhenJiLuZhuangTai);

                map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
                map.put("msg", "结束就诊");
            }
        }
        else if (jiuZhenJiLuZhuangTai == 2) {
            if(zBook == 0) {
                this.bingLiMapper.gengXinById(bingLi);
                List<ChuFang> chuFanggeng = bingLi.getChuFangList();
                for (int i = 0; i < chuFanggeng.size(); i++) {
                    ChuFang chuFang = chuFanggeng.get(i);
                    chuFang.setCiSu(Integer.parseInt(ciSu));
                    this.chuFangMapper.gengXinById(chuFang);
                }
                jiuZhenJiLuZhuangTai = 2;
                this.guaHaoJiLuMapper.gengXinById(GuaHaoDanHao, jiuZhenJiLuZhuangTai);
                map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
                map.put("msg", "更新成功");
            }
            if(zBook == 1) {
                this.bingLiMapper.gengXinById(bingLi);
                List<ChuFang> chuFanggeng = bingLi.getChuFangList();
                for (int i = 0; i < chuFanggeng.size(); i++) {
                    ChuFang chuFang = chuFanggeng.get(i);
                    chuFang.setCiSu(Integer.parseInt(ciSu));
                    this.chuFangMapper.gengXinById(chuFang);
                }
                jiuZhenJiLuZhuangTai = 3;
                this.guaHaoJiLuMapper.gengXinById(GuaHaoDanHao, jiuZhenJiLuZhuangTai);
                map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
                map.put("msg", "结束就诊成功");
            }
            }

        else{
            map.put("code",50000);
            map.put("msg","就诊状态错误，无法操作");
        }
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param bingLi 实例对象
     * @return 实例对象
     */
	@Override
    public Map<String, Object> gengXinById(BingLi bingLi) {
        this.bingLiMapper.gengXinById(bingLi);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);   // 前端端分离时，前端人员会首先判断co de值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "更新成功");
        return map;
    }


}